package com.ctg.itrdc.k8sconsole.control;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.data.dto.PodQueryDTO;
import com.ctg.itrdc.k8sconsole.service.IPodService;
import com.ctg.itrdc.paspsdk.common.utils.json.JSONUtils;

/**
 * 
 * @description: 
 * @version: v1.0.0 
 * @author: Piao.Wu
 * 
 * Modification History:
 * Date               Author      Version     Description
 * -----------------------------------------------------------------
 * 2016年8月24日 上午10:51:09     Piao.Wu       v1.0.0        create
 *
 */
@Controller
@RequestMapping("/pod")
public class PodController {

	@Resource
	private IPodService podService;
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	
	@RequestMapping(value = "/queryPodsByNode", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryPodsByNode(@RequestParam("clusterCode") String clusterCode,
			 @RequestParam("perpage") String perpage
			,@RequestParam("page") String page
			,@RequestParam("nodeip") String nodeip) {
		
		Map<String, Object> map = null;
		String jsonStr = null;
		for(int i=0;i<3;i++){
			try {
				jsonStr = podService.getPodsByNode(clusterCode,nodeip, perpage, page);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("服务器出现异常！",e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if(map==null||map.size()==0){
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}
	
	
	@RequestMapping(value = "/queryPodsByRc", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryPodsByRc(@RequestParam("clusterCode") String clusterCode,
			 @RequestParam("perpage") String perpage
			,@RequestParam("page") String page
			,@RequestParam(value="namesp",required=false) String namesp
			,@RequestParam(value="name",required=false) String name) {
		
		Map<String, Object> map = null;
		String jsonStr = null;
		for(int i=0;i<3;i++){
			try {
				jsonStr = podService.getPodsByRc(clusterCode,perpage, page, namesp, name);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("查询出错！",e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if(map==null||map.size()==0){
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}
	
	
	@RequestMapping(value = "/queryPodsByRs", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryPodsByRs(@RequestParam("clusterCode") String clusterCode,
			 @RequestParam("perpage") String perpage
			,@RequestParam("page") String page
			,@RequestParam(value="namesp",required=false) String namesp
			,@RequestParam(value="name",required=false) String name) {
		
		Map<String, Object> map = null;
		String jsonStr = null;
		for(int i=0;i<3;i++){
			try {
				jsonStr = podService.getPodsByRs(clusterCode,perpage, page, namesp, name);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("查询出错！",e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if(map==null||map.size()==0){
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}
	
	
	/**
	 * 
	 * @Description: 查询pod详情  TODO 此接口开发者门户也在调用
	 * @author Piao.Wu 2016年9月27日 上午9:14:51
	 * @param clusterCode
	 * @param namespace
	 * @param name
	 * @return
	 * CommonResult
	 *
	 */
	@RequestMapping(value = "/queryPodDetail", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryPodDetail(@RequestParam("clusterCode") String clusterCode,
			@RequestParam("namesp") String namesp,@RequestParam("name") String name) {
		
		Map<String, Object> map = null;
		String jsonStr = null;
		for(int i=0;i<3;i++){
			try {
				jsonStr = podService.getPodDetail(clusterCode,namesp,name);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("查询出错！",e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if(map==null||map.size()==0){
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}
	
	
	/**
	 * 
	 * @Description: 获取pod资源资源详情
	 * @author Piao.Wu 2016年10月27日 下午4:40:20
	 * @param podQueryDTO
	 * @return
	 * CommonResult
	 *
	 */
	@RequestMapping(value = "/queryPodResource", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult queryPodResource(@RequestBody PodQueryDTO podQueryDTO) {
		
		Map<String, Object> map = null;
		String jsonStr = null;
		for(int i=0;i<3;i++){
			try {
				jsonStr = podService.getPodResource(podQueryDTO);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("queryPodResource出错！",e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if(map==null||map.size()==0){
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}
	
	
}
